package com.dllo.lanou.cookapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.adapter.MyListCookAdapter;
import com.dllo.lanou.cookapp.adapter.ShowCookAdapter;
import com.dllo.lanou.cookapp.adapter.ShowCookingAdapter;
import com.dllo.lanou.cookapp.entity.AddCookStepEntity;
import com.dllo.lanou.cookapp.entity.Collection;
import com.dllo.lanou.cookapp.entity.Post;
import com.dllo.lanou.cookapp.entity.User;
import com.dllo.lanou.cookapp.fragment.PersonInfoFragment;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.listener.FindListener;

public class MyListCookActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private Intent intent;
    private String flag;
    private User user;
    private Handler handler;
    @Bind(R.id.pull_listview)
    PullToRefreshListView listview;
    @Bind(R.id.s_title)
    TextView title;
    @Bind(R.id.fl_layout)
    FrameLayout fl_layout;

    private List<Collection> collections = new ArrayList<>();
    private List<Post> posts = new ArrayList<>();
    private List<AddCookStepEntity> addCookStepEntities = new ArrayList<>();
    private MyListCookAdapter adapter;
    private ShowCookAdapter showCookAdapter;
    private ShowCookingAdapter showCookingAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list_cook);
        ButterKnife.bind(this);
        user = BmobUser.getCurrentUser(this, User.class);
        adapter = new MyListCookAdapter(this);

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(this);

        intent = getIntent();
        flag = intent.getStringExtra("flag");
        if (flag != null) {
            title.setText(flag);
        }
        switch (flag) {
            case "我的收藏":
                initMyCollection();
                break;
            case "我的厨说":
                initShowCook();
                break;
            case "我的菜谱":
                initShowCooking();
                break;
            default:

                break;
        }
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0) {
                    collections = (List<Collection>) msg.obj;
                    if (collections.size() == 0) {
                        fl_layout.setVisibility(View.VISIBLE);
                        listview.setVisibility(View.GONE);
                    } else {
                        listview.setVisibility(View.VISIBLE);
                        fl_layout.setVisibility(View.GONE);
                        Log.i("我的收藏", collections + "");
                        adapter.addData(collections);
                        adapter.notifyDataSetChanged();

                    }
                } else if (msg.what == 1) {
                    posts = (List<Post>) msg.obj;
                    if (posts.size() == 0) {
                        fl_layout.setVisibility(View.VISIBLE);
                        listview.setVisibility(View.GONE);
                    } else {
                        listview.setVisibility(View.VISIBLE);
                        fl_layout.setVisibility(View.GONE);
                        //Log.i("我的收藏", collections + "");
                        showCookAdapter = new ShowCookAdapter(MyListCookActivity.this);
                        listview.setAdapter(showCookAdapter);
                        Log.i("作者名字+++", posts.get(0).getAuthor().getUsername() + "");
                        showCookAdapter.addData(posts);
                        //showCookAdapter.notifyDataSetChanged();
                    }
                } else if (msg.what == 2) {
                    addCookStepEntities = (List<AddCookStepEntity>) msg.obj;
                    if (addCookStepEntities.size() == 0) {
                        fl_layout.setVisibility(View.VISIBLE);
                        listview.setVisibility(View.GONE);
                    } else {
                        listview.setVisibility(View.VISIBLE);
                        fl_layout.setVisibility(View.GONE);
                        //Log.i("我的收藏", collections + "");
                        showCookingAdapter = new ShowCookingAdapter(MyListCookActivity.this);
                        listview.setAdapter(showCookingAdapter);
                        Log.i("作者名字+++", addCookStepEntities.get(0).getAuthor().getUsername() + "");
                        showCookingAdapter.addData(addCookStepEntities);
                        //showCookAdapter.notifyDataSetChanged();
                    }
                }
            }


        };
    }

    private void initShowCooking() {
        BmobQuery<AddCookStepEntity> cooking_query = new BmobQuery<AddCookStepEntity>();
        cooking_query.addWhereEqualTo("author", user);
        cooking_query.include("author");
        cooking_query.setLimit(20);
        cooking_query.findObjects(this, new FindListener<AddCookStepEntity>() {
            @Override
            public void onSuccess(List<AddCookStepEntity> list) {
                Collections.reverse(list);//倒叙
                Message msg = new Message();
                msg.what = 2;
                msg.obj = list;
                handler.sendMessage(msg);
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    private void initShowCook() {
        BmobQuery<Post> post_query = new BmobQuery<Post>();
        post_query.addWhereEqualTo("author", user);
        post_query.include("author");
        post_query.setLimit(20);
        post_query.findObjects(this, new FindListener<Post>() {
            @Override
            public void onSuccess(List<Post> list) {
                Collections.reverse(list);//倒叙
                Message msg = new Message();
                msg.what = 1;
                msg.obj = list;
                handler.sendMessage(msg);
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    //我的收藏
    private void initMyCollection() {
        BmobQuery<Collection> co_query = new BmobQuery<Collection>();
        co_query.addWhereEqualTo("user", new BmobPointer(user));
        co_query.setLimit(20);
        co_query.include("addCookStepEntity.author");
        co_query.findObjects(this, new FindListener<Collection>() {
            @Override
            public void onSuccess(List<Collection> list) {
                Collections.reverse(list);//倒叙
                Log.i("作者名字++", list.get(0).getAddCookStepEntity().getName());
                Message msg = new Message();
                msg.what = 0;
                msg.obj = list;
                handler.sendMessage(msg);
            }

            @Override
            public void onError(int i, String s) {

            }
        });

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        position = position - 1;
        AddCookStepEntity addCookStepEntity = new AddCookStepEntity();
        if ("我的收藏".equals(flag)) {
            Collection collection = collections.get(position);
            addCookStepEntity = collection.getAddCookStepEntity();
        } else if ("我的菜谱".equals(flag)) {
            addCookStepEntity = addCookStepEntities.get(position);
        }
        if (addCookStepEntity != null) {
            Intent intent = new Intent(this, ShowCookingDesActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("entity", addCookStepEntity);
            intent.putExtras(bundle);
            startActivity(intent);
            Toast.makeText(this, "第" + position, Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(this, "点击失败" , Toast.LENGTH_LONG).show();
        }
    }
}
